package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.CartProductVO;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/cart")
public class FrontCateController {
	@Autowired
	private ICartService cartService;
	
	@RequestMapping("/insertToCart")
	@ResponseBody
	public JSONResult insertToCart(Cart cart , HttpSession session) {
		System.out.println("FrontCateController.insertToCart()");
		User user = (User) session.getAttribute("user");
		cart.setUserId(user.getId());
		int result = cartService.insertToCart(cart);
		
		return result > 0 ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
	}
	
	@RequestMapping("/getCartListPage")
	public String getCartListPage(Model model , HttpSession session) {
		System.out.println("FrontCateController.getCartListPage()");
		User user = (User) session.getAttribute("user");
		List<CartProductVO> list = cartService.getCartListPage(user.getId());
		model.addAttribute("list", list);
		
		return "/jsp/front/cart.jsp";
	}
	
	@RequestMapping("/deleteByCartId")
	@ResponseBody
	public JSONResult deleteByCartId(Integer id) {
		System.out.println("FrontCateController.deleteByCartId()");
		cartService.deleteByCartId(id);
		
		return JSONResult.ok("确定不要来么？好可惜啊");
	}
	
	@RequestMapping("/changeValue")
	@ResponseBody
	public JSONResult changeValue(Integer id , Integer count) {
		System.out.println("FrontCateController.changeValue()");
		cartService.changeValue(id,count);
		
		return JSONResult.ok();
	}
}
